//
//  SEExamineReserveViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/14.
//

import HandyJSON

enum SEExamineReserveEyeType: Int {
    case left = 1
    case right
    case both

    var name: String {
        switch self {
        case .left: return "左眼"
        case .right: return "右眼"
        case .both: return "双眼"
        }
    }
}

class SEExamineReserveViewModel: SEBaseViewModel {

    var models = [SEExamineReserveModel]()

    convenience init(models: [SEExamineReserveModel]) {
        self.init()
        self.models = models
    }

    var hospitalModel = SEHospitalListItem()

    var checkSource: Int = 1

    var patientItem = SEPatientDetailViewModel()

    var applyCheckTime: String = ""

    var applyFormId: String = ""

    var submitHandler: ((SEHttpStatus) -> Void)?

    var totalFee: Double {
        var total: Double = 0
        selectedItemArray.forEach {
            total += $0.checkFee
        }
        return total
    }

    var checkEyeType: SEExamineReserveEyeType = .both {
        didSet {
            for i in 0..<models.count {
                if models[i].checkEyePart != checkEyeType.rawValue {
                    for j in 0..<models[i].checkItemFeeDTOList.count {
                        models[i].checkItemFeeDTOList[j].isSelected = false
                    }
                }
            }
        }
    }

    var checkItemArray: [SEExamineReserveCheckItem] {
        for item in models {
            if item.checkEyePart == checkEyeType.rawValue {
                return item.checkItemFeeDTOList
            }
        }
        return []
    }

    var selectedItemArray: [SEExamineReserveCheckItem] {
        var selectedArray: [SEExamineReserveCheckItem] = []
        for model in models {
            if model.checkEyePart == checkEyeType.rawValue {
                for item in model.checkItemFeeDTOList {
                    if item.isSelected {
                        selectedArray.append(item)
                    }
                }
            }
        }
        return selectedArray
    }

    func checkItem(indexPath: IndexPath) -> SEExamineReserveCheckItem {
        return checkItemArray[indexPath.item]
    }

    func replaceCheckItem(indexPath: IndexPath, itemModel: SEExamineReserveCheckItem) {
        guard models.count > 0 else { return }
        var index = 0
        for i in 0..<models.count {
            if models[i].checkEyePart == checkEyeType.rawValue {
                index = i
                break
            }
        }
        models[index].checkItemFeeDTOList[indexPath.item] = itemModel
    }

    var entranceModelArray: [SERegistrationConfirmPatientCellModel] {
        return [SERegistrationConfirmPatientCellModel(titleText: "选择就诊人",
                                                      memoPlaceHolder: "请选择",
                                                      memoText: patientItem.model.name,
                                                      isHiddenArrow: false),
                SERegistrationConfirmPatientCellModel(titleText: "选择检查时间",
                                                      memoPlaceHolder: "请选择",
                                                      memoText: applyCheckTime,
                                                      isHiddenArrow: false),
            ]
    }

    var checkSubmit: String? {
        if selectedItemArray.isEmpty {
            return "请选择检查项目"
        }
        if patientItem.model.id.isEmpty {
            return "请选择就诊人"
        }
        if applyCheckTime.isEmpty {
            return "请选择检查时间"
        }
        return nil
    }

    /// APIRequest
    /// 医院检查项目
    func requestExamineItems() {
        SEHttpManager.shared.requestForModelArray(SEExamineAPI.hospitalExamineItems(hospitalId: hospitalModel.orgId), model: SEExamineReserveModel.self, withLoading: true) { (models, status) in
            switch status {
            case .success:
                self.models = models ?? []
            default: break
            }
            self.updateData?(status)
        }
    }

    /// 提交预约检查
    func requestSubmitExamine() {
        if let msg = checkSubmit {
            SEHUD.showError(message: msg)
            return
        }
        SEHttpManager.shared.requestForAny(SEExamineAPI.submitExamine(examine: self, hospital: hospitalModel, patient: patientItem, checkSource: checkSource), withLoading: true) { (result, status) in
            self.applyFormId = result as? String ?? ""
            self.submitHandler?(status)
        }
    }
}

struct SEExamineReserveModel: HandyJSON {
    var checkEyePart: Int = 0
    var checkItemFeeDTOList: [SEExamineReserveCheckItem] = []
}

struct SEExamineReserveCheckItem: HandyJSON {
    /**
     单项检查id
     */
    var formDetailId: String = ""
    /**
     检查项费用
     */
    var checkFee: Double = 0
    /**
     检查项编码
     */
    var checkItemCode: String = ""
    /**
     检查项名字
     */
    var checkItemValue: String = ""
    /**
     检查单项是否可以取消 - 1: 可以 2: 不可以
     */
    var canCancelDetail: Int = 2
    /**
     是否选中, 本地字段
     */
    var isSelected: Bool = false
}

